jsMath

treca

# Kratki osvrt na liste 
       
t1 = [0,1,2,3,4]; t2 = [5,6,7,8,6]; z=zip(t1,t2); z 
       
[(0, 5), (1, 6), (2, 7), (3, 8), (4, 6)]
mul(t2); add(t1); print 22 not in t1 
       
10080
10
True
[i^2 for i in t1 if 0<i<4] 
       
[1, 4, 9]
for (a,b) in zip(t1,t2): print a,"...",b+1 # forall 
       
0 ... 6
1 ... 7
2 ... 8
3 ... 9
4 ... 7
# Funkcije 
       
f(x)=x^2; type(f) 
       
<type 'sage.symbolic.expression.Expression'>
f1=diff(f); f1(3) 
       
6
diff(f)(3) # diff(f,x) 
       
6
plot(f1,0,1) 
       
f1=f.derivative(); f1(3) 
       
6
type(f1) 
       
<type 'sage.symbolic.expression.Expression'>
def g(x): return x^2 
       
g(2) 
       
4
type(g) 
       
<type 'function'>
g1=g.derivative() 
       
Traceback (click to the left of this block for traceback)
...
AttributeError: 'function' object has no attribute 'derivative'
var("y"); plot(g(y),y,0,1) 
       
y
clear_vars(y); plot(g(y),y,0,1) 
       
Traceback (click to the left of this block for traceback)
...
TypeError: clear_vars() takes no arguments (1 given)
f(x)=x; g=f.derivative(); g 
       
x |--> 1
g(3) 
       
1
f=x; #x^3 g=f.derivative(); g 
       
1
g(3) 
       
Traceback (click to the left of this block for traceback)
...
ValueError: the number of arguments must be less than or equal to 0
g(x=3) 
       
1
# http://www.sagemath.org/doc/tutorial/tour_functions.html 
       
def h(x): if x<2: return x^2 else: return x 
       
plot(h,x,1,3,exclude=[2]) 
       
plot(h(x),x,1,3) 
       
type(x<2) 
       
<type 'sage.symbolic.expression.Expression'>
# Primjer 
       
def f(n): p=list_plot([[i,primes_first_n(i)[-1]/i] for i in range(1,n)]); return p 
       
f(20) 
       
def g(n): p=list_plot([[i,primes_first_n(i)[-1]/i] for i in range(1,n)]); show(p); return 
       
g(20) 
       
# Rekurzivne funkcije 
       
def fr(n): if n==0: return 1 else: return n*fr(n-1); 
       
fr(5) 
       
120
def tt1(k): t=cputime(); [fr(500) for i in range(1,k)]; return cputime(t) 
       
tt1(1000) 
       
0.46402800000000011
def tt2(k): t=cputime(); [factorial(500) for i in range(1,k)]; return cputime(t) 
       
tt2(1000) 
       
0.0080009999999997028